/*
  This file is part of an addition to the model that reads, writes, and displays information
  This particular file reads, writes, and displays simulation information.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ssm.h"

//This function writes the simulation information to a file, most of the time, this file is called sim.str
//returns 0 on a sucsessful write
void printSim(Simulation source)
{
	printf("dt     = %f\n", source.dt);
       	printf("t      = %f\n", source.t);
	printf("imax   = %d\n", source.imax);
	printf("iprint = %d\n", source.iprint);
}

//these are obvious reader and writer functions
int writeSim(char* filename, Simulation *source)
{
	FILE *fp;
	if((fp=fopen(filename, "wb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for output\n", filename);
  		exit(1);
	}
	if((fwrite(source, sizeof(Simulation), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}

//Again, obviously a reader function
int readSim(char* filename, Simulation *dest)
{
	FILE *fp;
	if((fp=fopen(filename,"rb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for input\n", filename);
  		exit(1);
	}
	if((fread(dest, sizeof(Simulation), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}
